package com.foxmail.yusp75.myexam;

import android.content.Context;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;


public class PaperListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<String> objects;
    private View.OnClickListener listener_del;
    private View.OnClickListener listener_overview;

    public PaperListAdapter(@NonNull Context context, View.OnClickListener listener_del,
                            View.OnClickListener listener_overview, @NonNull ArrayList<String> objects) {
        super();
        this.context = context;
        this.objects = objects;
        this.listener_del = listener_del;
        this.listener_overview = listener_overview;
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(context).inflate(R.layout.paper_list_adapter, null);
            holder = new ViewHolder();
            holder.title = view.findViewById(R.id.title);
            holder.overview = view.findViewById(R.id.overview);
            holder.trash = view.findViewById(R.id.trash);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }

        holder.title.setText(objects.get(position));
        holder.overview.setTag(position);
        holder.trash.setTag(position);

        // item selected
        if (convertView != null) {
            if (convertView.isSelected())
                convertView.setBackgroundColor(Color.RED);
            else
                convertView.setBackgroundColor(Color.WHITE);
        }
        // click
        holder.overview.setOnClickListener(listener_overview);
        holder.trash.setOnClickListener(listener_del);

        return view;
    }


    private class ViewHolder {
        TextView title;
        ImageView overview;
        ImageView trash;
    }
}
